package CSS::Builder::Property;
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::AttributeHelpers;

use CSS::Builder::Style;
use CSS::Builder::Selector;

use CSS::Parser::AsBuilder::Selectors;
use CSS::Parser::AsBuilder::Styles;

extends 'CSS::Builder::Base';
with 'CSS::Builder::Roles::List';

has 'selectors' => (
        metaclass => 'Collection::Array',
        is      => 'rw' ,
        isa     => 'ArrayRef[Object]',
        lazy    => 1,
        default => sub { [] },
        provides  => {
            'push' => 'push_selector',
            'pop'  => 'pop_selector',
        }
);

sub add_selector {
    my ($self , $selector) = @_;
    
    # parse selector
    my $sel_parser = CSS::Parser::AsBuilder::Selectors->new;
    my $res = $sel_parser->build_selectors($selector);
    
    # use only first result, even if user passed many selectors
    $self->push_selector($res->[0]);
}

sub add_style {
    my ($self , $style) = @_;
    
    # parse value
    my $sty_parser = CSS::Parser::AsBuilder::Styles->new;
    my $res = $sty_parser->build_styles($style);
    
    # save all
    $self->push(@$res);
}

sub css {
    my $self = shift;
    
    my $sel = $self->docss($self->selector);
    
    my @styles;
    foreach (@{$self->list}) {
        push @styles , $self->docss($_);
    }
    
    print $sel , " {\n";
    print "\t$_;\n" foreach @styles;
    print "}\n\n";
}

1;